package com.incross.dawin.http.parser;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import com.incross.dawin.util.CommonUtils;

import android.util.Log;

public class MediaFileInfoParser {
	
	private String mMediaFileUrl = "";
	private String mMediaDelivery = "";
	private String mMediaType = "";
	private String mThumbnailUrl = "";
	private int mMediaBitrate = -1;
	private int mMediaWidth = -1;
	private int mMediaHeight = -1;
	
	// 20141216 파싱시 발생한 문제 체크용 boolean값 추가
	private boolean mIsParsingProblemOccured = false;
	
	public MediaFileInfoParser(NodeList list) {
		if(list != null) {
			try {
				NodeList child = null;
				for(int i = 0; i < list.getLength(); i++) {
					if(list.item(i) != null) {
						child = list.item(i).getChildNodes();
						break;
					}
				}
				
				for(int i = 0; i < child.getLength(); i++) {
					if(child.item(i).getFirstChild() != null) {
						if(child.item(i).getNodeName().equals("MediaFile")) {
							mMediaFileUrl = child.item(i).getFirstChild().getNodeValue();
							
							NamedNodeMap attributes = child.item(i).getAttributes();
							for(int j = 0; j < attributes.getLength(); j++) {
								String attrName = attributes.item(j).getNodeName();
								String attrValue = attributes.item(j).getNodeValue();
								
								if(attrName.equals("delivery")) {
									mMediaDelivery = attrValue;
								} else if(attrName.equals("type")) {
									mMediaType = attrValue;
								} else if(attrName.equals("bitrate")) {
									mMediaBitrate = (CommonUtils.isNumber(attrValue)? Integer.parseInt(attrValue): -1);
								} else if(attrName.equals("width")) {
									mMediaWidth = (CommonUtils.isNumber(attrValue)? Integer.parseInt(attrValue): -1);
								} else if(attrName.equals("height")) {
									mMediaHeight = (CommonUtils.isNumber(attrValue)? Integer.parseInt(attrValue): -1);
								} else if(attrName.equals("thumb")) {
									mThumbnailUrl = attrValue;
								}
							}
							break;
						}
					}
				}
			} catch(Exception e) {
				e.printStackTrace();
				mIsParsingProblemOccured = true;
			}
		} else {
			mIsParsingProblemOccured = true;
		}
	}
	
	// 20141216 파싱시 문제가 발생한 경우 체크를 위한 메소드 추가
	public boolean isParsingProblemOccured() {
		return mIsParsingProblemOccured;
	}
	
	public String getMediaFileUrl() {
		return mMediaFileUrl;
	}
	
	public String getMediaDelivery() {
		return mMediaDelivery;
	}
	
	public String getMediaType() {
		return mMediaType;
	}
	
	public int getMediaBitrate() {
		return mMediaBitrate;
	}
	
	public int getMediaWidth() {
		return mMediaWidth;
	}
	
	public int getMediaHeight() {
		return mMediaHeight;
	}
	
	public String getMediaThumbnailUrl() {
		return mThumbnailUrl;
	}
	
	

}
